package tests.update;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import model.Budget;
import model.BudgetFactory;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.workplicity.util.Helper;
import org.workplicity.worklet.WorkletContext;

import cc.test.bridge.BudgetInterface;

import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;

/*
 * 
 * Attempts to change the description of an existing budget
 * 
 */

public class BudgetUpdateDescriptionTest00
{
	Budget budget;
	BudgetFactory factory;

	@Before
	public void setUp() throws Exception
	{
		System.out.println("Begining test: " +this.getClass());
		System.out.println("Setup");
		
		factory = new BudgetFactory();
		
		//create budget
		budget = (Budget) factory.create();
        budget.setName("Test budget");
        budget.setDescription("This is an update test budget.");
        budget.commit();
	}

	@After
	public void tearDown() throws Exception
	{
		System.out.println("Tear down");
		
		Helper.delete(budget, "Budgets", WorkletContext.getInstance());
	}

	@Test
	public void test() throws Exception
	{
		try
		{
			budget.setDescription("This is my updated budget");
			budget.commit();
        
			BasicDBObject criteria = new BasicDBObject("entry.id", budget.getId());
			String predicate = JSON.serialize(criteria);

			@SuppressWarnings("unchecked")
			ArrayList<BudgetInterface> budgets = Helper.query("Budgets", predicate, WorkletContext.getInstance());
			
			if( !((Budget)budgets.get(0)).getDescription().equals("This is my updated budget"))
			{
				fail("Update budget name failed");
			}
		}
		catch (Exception e)
		{
			fail("Updating budget name failed with exception " + e);
		}
	}
}